<template>
  <div class="app-container">
    <el-form ref="formRef" :model="form" label-width="110px">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="出入仓类型" prop="type">
            <dict-tag :options="inout_storehouse" :value="form.type" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="出仓单编号" prop="code">
            <el-tooltip effect="dark" :content="form.code">
              <div class="form-text">{{ form.code }}</div>
            </el-tooltip>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="出仓名单称" prop="name">
            <el-tooltip effect="dark" :content="form.name">
              <div class="form-text">{{ form.name }}</div>
            </el-tooltip>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="工厂" prop="factoryCode">
            <div class="form-text">{{ form.factoryName }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工段" prop="workshopCode">
            <div class="form-text">{{ form.workShopName }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="产线" prop="lineCode">
            <div class="form-text">{{ form.lineName }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工艺段" prop="sectionCode">
            <div class="form-text">{{ form.sectionName }}</div>
          </el-form-item>
        </el-col>
        <el-form-item label="出仓时间" prop="launchTime">
          <el-tooltip effect="dark" :content="form.launchTime">
            <div class="form-text">{{ form.launchTime }}</div>
          </el-tooltip>
        </el-form-item>
        <el-col :span="6">
          <el-form-item label="确认出仓人" prop="launchPerson">
            <el-tooltip effect="dark" :content="form.launchPerson">
              <div class="form-text">{{ form.launchPerson }}</div>
            </el-tooltip>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="备注" prop="remark">
            <div class="form-text">{{ form.remark }}</div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-tabs v-model="activeName" class="demo-tabs">
      <el-tab-pane label="出仓明细" name="first">
        <el-form
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          label-width="100px"
          class="form-search"
        >
          <el-form-item label="ERP订单编号" prop="orderCode">
            <el-input
              v-model="queryParams.orderCode"
              placeholder="请输入ERP订单编号"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="产品名称" prop="productName">
            <el-input
              v-model="queryParams.productName"
              placeholder="请输入产品名称"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="物料名称" prop="whName">
            <el-input
              v-model="queryParams.whName"
              placeholder="请输入物料名称"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="批次号" prop="batchNo">
            <el-input
              v-model="queryParams.batchNo"
              placeholder="请输入批次号"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="线边仓" prop="storehouseCode">
            <StoreSelect
              v-model="queryParams.storehouseCode"
              :line="form.lineCode"
              :section="form.sectionCode"
              placeholder="请选择线边仓"
            ></StoreSelect>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="search" @click="handleQuery">查询</el-button>
            <el-button icon="refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-form ref="tableForm" :model="whStorehouseInDetailList" label-width="0">
          <el-table
            :data="whStorehouseInDetailList"
            :row-class-name="rowWhStorehouseInDetailIndex"
            border
            maxHeight="400"
            cell-class-name="table-form-item"
            ref="whStorehouseInDetail"
          >
            <el-table-column label="序号" align="center" prop="index" width="80" />
            <!-- <el-table-column label="ERP订单编号" prop="ErpCode" width="150">
              <template #default="scope">
                <el-form-item label="">
                  <el-input
                    v-model="scope.row.orderCode"
                    placeholder="请输入订单编码"
                  />
                </el-form-item>
              </template>
            </el-table-column> -->
            <el-table-column label="ERP订单编号" prop="orderCode" width="150" />

            <el-table-column label="产品名称" prop="productName" width="150"></el-table-column>
            <el-table-column label="产品编号" prop="bomCode" width="150" />

            <el-table-column label="物料编号" prop="whCode" width="150" />

            <el-table-column label="物料名称" prop="whName" width="150" />

            <el-table-column label="批次号" prop="batchNo" width="150" />

            <el-table-column label="单位" prop="unit" width="150" />
            <el-table-column label="线边仓" prop="storehouseName" width="150"></el-table-column>
            <el-table-column label="库位" prop="binName" width="150"></el-table-column>
            <el-table-column label="库存" prop="stockNum" width="150"></el-table-column>
            <el-table-column label="数量" prop="deliveryNumber" width="150" />
          </el-table>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup name="warehouse">
import { getWhStorehouseOut } from "@/api/warehouse/WhStorehouseOut";
import { listWhStorehouseOutdetail } from "@/api/warehouse/WhStorehouseOutdetail";
import { reactive, toRefs, useAttrs, getCurrentInstance, ref, onMounted } from "vue";
const attrs = useAttrs();
const { proxy } = getCurrentInstance();
const activeName = ref("first");
let { inout_storehouse } = proxy.useDict("inout_storehouse");
const { prod_inout_storehouse } = proxy.useDict("prod_inout_storehouse");
inout_storehouse.value =  [...new Set([...inout_storehouse.value,...prod_inout_storehouse.value])]; 
const isBatch = ref(false);
const allTableData = ref([]);
const refData = reactive({
  // 表单参数
  form: {},
  queryParams: {
    batchNo: "",
    matName: "",
    matCode: "",
    matTypeName: "",
  },
});
const { form, queryParams } = toRefs(refData);
const emit = defineEmits(["close", "setActions", "setIsActionTop"]);
const whStorehouseInDetailList = ref([]);
const formRef = ref("formRef");
const tableForm = ref("tableForm");
const actions = ref([]);

// 表单重置
function reset() {
  form.value = {
    id: "",
    type: "2",
    code: "",
    name: "",
    factoryCode: "",
    workshopCode: "",
    lineCode: "",
    sectionCode: "",
    storehouseCode: "",
  };
  formRef.value.resetFields();
  whStorehouseInDetailList.value = [];
  allTableData.value = [];
}

/** 修改按钮操作 */
function getDetail(id) {
  getWhStorehouseOut(id).then((response) => {
    form.value = response.data;
    getListWhStorehouseInDetail(response.data.code);
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  filterSubTable();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  filterSubTable();
}
function filterSubTable() {
  const result = [];
  allTableData.value.forEach((el) => {
    if (
      ((queryParams.value.orderCode && el.orderCode === queryParams.value.orderCode) ||
        !queryParams.value.orderCode) &&
      ((queryParams.value.productName && el.productName.includes(queryParams.value.productName)) ||
        !queryParams.value.productName) &&
      ((queryParams.value.batchNo && el.batchNo === queryParams.value.batchNo) ||
        !queryParams.value.batchNo) &&
      ((queryParams.value.storehouseCode &&
        el.storehouseCode === queryParams.value.storehouseCode) ||
        !queryParams.value.storehouseCode) &&
      ((queryParams.value.whName && el.matTypeName.includes(queryParams.value.whName)) ||
        !queryParams.value.whName)
    ) {
      result.push(el);
    }
  });
  whStorehouseInDetailList.value = result;
}
/** 物料入仓明细序号 */
function rowWhStorehouseInDetailIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

function getListWhStorehouseInDetail(code) {
  listWhStorehouseOutdetail({ pageSize: 10000, pageNum: 1, outStoreCode: code }).then((res) => {
    if (isBatch.value) {
      whStorehouseInDetailList.value.push(...res.rows);
      allTableData.value.push(...res.rows);
    } else {
      whStorehouseInDetailList.value = res.rows;
      allTableData.value = res.rows;
    }
  });
}

onMounted(() => {
  reset();
  const { data } = attrs;

  emit("setActions", actions.value);

  if (data) {
    getDetail(data.mainTableId);
  }
});
</script>

<style lang="scss">
.form-text {
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
